package com.luo.gmall.ums.service.impl;

import com.luo.gmall.ums.form.RegisterForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.PageParamVo;

import com.luo.gmall.ums.mapper.UserMapper;
import com.luo.gmall.ums.entity.UserEntity;
import com.luo.gmall.ums.service.UserService;

import java.util.Date;
import java.util.UUID;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<UserEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<UserEntity>()
        );

        return new PageResultVo(page);
    }

    /**
     * @param data 要校验的数据
     * @param type 要校验的数据类型：1，用户名；2，手机；3，邮箱
     * @return 返回检验结果
     */
    @Override
    public Boolean dataCheck(String data, Integer type) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        switch (type) {
            case 1:
                queryWrapper.eq("username", data);
                break;
            case 2:
                queryWrapper.eq("phone", data);
                break;
            case 3:
                queryWrapper.eq("email", data);
                break;
            default:
                return false;

        }
        Integer count = baseMapper.selectCount(queryWrapper);
        return count == 0;
    }

    /**
     * @param phone 用户的手机号码
     */
    @Override
    public void sendCode(String phone) {
        // TODO 发送短信的服务
    }

    /**
     * @param userEntity 注册用户对象
     */
    @Override
    public void register(UserEntity userEntity, String code) {
        // TODO 1.校验验证码是否正确 redis code

        // 2. 加盐加密
        String salt = StringUtils.substring(UUID.randomUUID().toString(), 0, 6);
        userEntity.setSalt(salt);
        userEntity.setPassword(DigestUtils.md5Hex(userEntity.getPassword() + salt));

        userEntity.setLevelId(1L);
        userEntity.setNickname(userEntity.getUsername());
        userEntity.setSourceType(1);
        userEntity.setIntegration(1000);
        userEntity.setGrowth(1000);
        userEntity.setStatus(1);
        userEntity.setCreateTime(new Date());
        // 3. 新增用户
        baseMapper.insert(userEntity);

        // TODO 删除redis中的验证码
    }

    /**
     * @param loginName 用户名/手机号/邮箱，格式为4~30位字母、数字、下划线
     * @param password  用户密码，格式为4~30位字母、数字、下划线
     * @return 返回user的JSON数据
     */
    @Override
    public UserEntity queryUser(String loginName, String password) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        // 根据loginName查询出是否有该用户
        queryWrapper.eq("username", loginName)
                .or().eq("phone", loginName)
                .or().eq("email", loginName);
        UserEntity userEntity = baseMapper.selectOne(queryWrapper);
        if (userEntity == null) {
            return userEntity;
        }
        // 对用户输入的密码进行加盐加密
        String saltPassword = DigestUtils.md5Hex(password + userEntity.getSalt());
        if (userEntity.getPassword().equals(saltPassword)) {
            return userEntity;
        }
        return null;
    }

}